package com.neo.springboot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neo.springboot.damain.User;
import com.neo.springboot.repository.UserRepository;

@RestController
public class UserController {

	@Autowired
	private UserRepository userRepository;
	
	@RequestMapping("/getUser")
	@Cacheable(value="function:user", key = "'getuser'")
	public User getUser(){
		User user = userRepository.findByUsername("bb1");
		System.out.println("test success");
		return user;
	}
	
	@RequestMapping("/getUsers")
	@Cacheable(value="function:user", key = "'getusers'")
	public List<User> getUsers(){
		List<User> users = userRepository.findAll();
		System.out.println("test users success");
		return users;
	}
	
	@RequestMapping("/userTest")
	@CacheEvict(value = "function:user", key = "'getuser'")
	public List<User> userTest(){
		/*long count = userRepository.count();
		System.out.println("count: " + count);
		boolean exists = userRepository.exists(Long.valueOf(5L));
		System.out.println("exists id=5 ? " + exists);
		User user1 = userRepository.findOne(Long.valueOf(5L));
		System.out.println("user1 : " + user1.toString());
		long count1 = userRepository.countByUsername("aa1");
		System.out.println("username = aa1 counts : " + count1);
		User user2 = userRepository.findByUsernameIgnoreCase("BB1");
		System.out.println("user2 : " + user2.toString());*/
		
		/*int page = 1, size = 3;
		Sort sort = new Sort(Direction.DESC, "id");
		Pageable pageable = new PageRequest(page, size, sort);
		Page<User> pages = userRepository.findAll(pageable);
		System.out.println(pages.nextPageable());
		userRepository.modifyByIdAndUsername("abc", Long.valueOf(2L));*/
		
		List<User> user = userRepository.findByEmailLike("%@%");
//		User user = new User();
//		user.setEmail("3@3");
//		user.setNickname("3");
//		user.setPassword("222222");
//		user.setRegTime("2019");
//		user.setUsername("cc1");
//		userRepository.save(user);
		return user;
	}
}
